<template>
  <div class="search">
    <!-- 搜索框 -->
    <div class="search-input-wrapper">
      <search-input v-model="query"></search-input>
    </div>

    <!-- 下方区域 -->
    <scroll ref="scrollRef" class="search-content" v-show="!query">
      <div>
        <!-- 热门搜索 -->
        <div class="hot-keys">
          <h1 class="title">热门搜索</h1>
          <ul>
            <li
              class="item"
              v-for="item in hotKeys"
              :key="item.id"
              @click="addQuery(item.first)"
            >
              <span>{{ item.first }}</span>
            </li>
          </ul>
        </div>
        <!-- 搜索历史 -->
        <div class="search-history" v-show="searchHistory.length">
          <h1 class="title">
            <span class="text">搜索历史</span>
            <span class="clear" @click="showConfirm">
              <i class="icon-clear"></i>
            </span>
          </h1>
          <confirm
            ref="confirmRef"
            text="是否清空所有搜索历史"
            confirm-btn-text="清空"
            @confirm="clearSearch"
          >
          </confirm>
          <search-list
            :searches="searchHistory"
            @select="addQuery"
            @delete="deleteSearch"
          ></search-list>
        </div>
      </div>
    </scroll>

    <!-- 搜索结果 -->
    <div class="search-result" v-show="query">
      <suggest
        :query="query"
        @select-song="selectSong"
        @select-singer="selectSinger"
      ></suggest>
    </div>
    <router-view v-slot="{ Component }">
      <transition appear name="slide">
        <component :is="Component" :data="selectedSinger" />
      </transition>
    </router-view>
  </div>
</template>

<script>
import SearchInput from "@/components/search/search-input.vue";
import SearchList from "@/components/base/search-list/search-list.vue";
import { ref, watch, onMounted, computed, nextTick } from "vue";
import { getHotSearch } from "@/api/index";
import suggest from "@/components/search/suggest.vue";
import { useStore } from "vuex";
import { useRouter } from "vue-router";
import Confirm from "@/components/base/confirm/confirm.vue";
import useSearchHistory from "@/components/search/use-search-history";
import Scroll from "@/components/base/scroll/scroll.vue";
export default {
  components: {
    SearchInput,
    suggest,
    SearchList,
    Scroll,
    Confirm,
  },
  setup() {
    const store = useStore();
    const router = useRouter();
    const { saveSearch, deleteSearch, scrollRef, clearSearch } =
      useSearchHistory();
    const query = ref("");
    const hotKeys = ref([]);
    const selectedSinger = ref(null);
    const confirmRef = ref(null);
    const searchHistory = computed(() => store.state.searchHistory);
    onMounted(async () => {
      const { data: res } = await getHotSearch();
      hotKeys.value = res.result.hots;
    });

    watch(query, async (newQuery) => {
      if (!newQuery) {
        await nextTick();
        refreshScroll();
      }
    });
    //刷新
    function refreshScroll() {
      scrollRef.value.scroll.refresh();
    }
    //输入框添加点击项
    function addQuery(key) {
      query.value = key;
    }
    //点击选中歌曲
    function selectSong(song) {
      saveSearch(query.value);
      store.dispatch("addSong", song);
    }
    //点击查找歌手
    function selectSinger(singer) {
      saveSearch(query.value);
      selectedSinger.value = singer;
      router.push({
        path: `/singer/${singer.id}`,
      });
    }
    //弹出删除
    function showConfirm() {
      confirmRef.value.show();
    }

    return {
      query,
      hotKeys,
      addQuery,
      selectSong,
      selectSinger,
      selectedSinger,
      deleteSearch,
      scrollRef,
      searchHistory,
      showConfirm,
      confirmRef,
      clearSearch,
    };
  },
};
</script>

<style lang="scss" scoped>
.search {
  position: fixed;
  width: 100%;
  top: 88px;
  bottom: 0;
  display: flex;
  flex-direction: column;
  .search-input-wrapper {
    margin: 20px;
  }
  .search-content {
    flex: 1;
    overflow: hidden;
    .hot-keys {
      margin: 0 20px 20px 20px;
      .title {
        margin-bottom: 20px;
        font-size: $font-size-medium;
        color: $color-text-l;
      }
      .item {
        display: inline-block;
        padding: 5px 10px;
        margin: 0 20px 10px 0;
        border-radius: 6px;
        background: $color-highlight-background;
        font-size: $font-size-medium;
        color: $color-text-d;
      }
    }
    .search-history {
      position: relative;
      margin: 0 20px;
      .title {
        display: flex;
        align-items: center;
        height: 40px;
        font-size: $font-size-medium;
        color: $color-text-l;
        .text {
          flex: 1;
        }
        .clear {
          @include extend-click();
          .icon-clear {
            font-size: $font-size-medium;
            color: $color-text-d;
          }
        }
      }
    }
  }
  .search-result {
    flex: 1;
    overflow: hidden;
  }
}
</style>
